package per.lance.easytomcat.dao;

import per.lance.easytomcat.bean.Book;
import per.lance.easytomcat.database.DB;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

public class BookDao implements BaseDao<Book> {

    DB db = DB.getInstance();

    @Override
    public void add(Book entity) {
        db.getBooks().put(entity.getId(), entity);
    }

    @Override
    public void update(Book entity) {
        Book book = getById(entity.getId());
        if (book != null) {
            book.setName(entity.getName());
            book.setPrice(entity.getPrice());
        }
    }

    @Override
    public void deleteById(String id) {
        db.getBooks().remove(id);
    }


    @Override
    public Book getById(String id) {
        return db.getBooks().get(id);
    }

    @Override
    public List<Book> selectAll() {
        ArrayList<Book> books = new ArrayList<Book>();
        Set<String> set  = db.getBooks().keySet();
        set.stream().forEach((string) -> books.add(db.getBooks().get(string)));
        return books;
    }
}
